package com.hfdg.authority.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfdg.prototype.base.utils.TimeUtils;
import com.hfdg.authority.mapper.AuthRoleMenuMapper;
import com.hfdg.authority.entity.AuthRoleMenu;
import com.hfdg.authority.service.AuthRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @ClassName AuthRoleMenuServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/19 18:26
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Service
public class AuthRoleMenuServiceImpl implements AuthRoleMenuService {
    @Autowired
    AuthRoleMenuMapper authRoleMenuMapper;

    @Override
    public String insert(AuthRoleMenu authRoleMenu) {
        String uid = UUID.randomUUID().toString();
        authRoleMenu.setUid(uid);
        authRoleMenu.setDelFlag("0");
        authRoleMenu.setCreateTime(new Date());
        authRoleMenuMapper.insert(authRoleMenu);
        return uid;
    }

    @Override
    public Integer update(AuthRoleMenu authRoleMenu) {
        authRoleMenu.setUpdateTime(new Date());
        return authRoleMenuMapper.update(authRoleMenu);
    }

    @Override
    public Integer  delete(String uid) {
        return authRoleMenuMapper.delete(uid);
    }
    @Override
    public Integer batchDelete(String[] uids) {
            return authRoleMenuMapper.batchDelete(uids);
    }

    @Override
    public AuthRoleMenu get(String uid) {
        return authRoleMenuMapper.get(uid);
    }

    @Override
    public List<AuthRoleMenu> list(AuthRoleMenu authRoleMenu) {
    return  authRoleMenuMapper.list(authRoleMenu);
    }

    @Override
    public PageInfo<AuthRoleMenu> getPagingData(int offset, int limit,String orderBy, AuthRoleMenu authRoleMenu) {
        if(null==orderBy|| StringUtils.isBlank(orderBy)){
        PageHelper.startPage((offset/limit)+1,limit);
        }else{
        PageHelper.startPage((offset/limit)+1,limit,orderBy);
        }
        PageInfo<AuthRoleMenu> pageInfo =new PageInfo<>(authRoleMenuMapper.list(authRoleMenu));
            return pageInfo;
            }
    @Override
    public String insertOrUpdate(AuthRoleMenu authRoleMenu){
        if(null!=authRoleMenu.getUid()){
            this.update(authRoleMenu);
        }else{
            this.insert(authRoleMenu);
        }
        return authRoleMenu.getUid();
    }

    @Override
    public Integer undoAuthorize(String roleUid) {
        return authRoleMenuMapper.undoAuthorize(roleUid);
    }
}
